package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Portrait;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserService userService;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public List<Menu> getMenu(String username) throws Exception {
        User user = userService.findByUserName(username);
        //加唯一标识
        String menuRedis = jedisPool.getResource().get("menu"+user.getId()+username);
        if(StringUtils.isEmpty(menuRedis)){
            List<Menu> menus = menuDao.findByUserName(username);
            for (Menu menu : menus) {
                //查找子菜单
                Integer parentMenuId = menu.getParentMenuId();
                if(parentMenuId == null){
                    Integer id = menu.getId();
                    Map map = new HashMap();
                    map.put("id",id);
                    map.put("username",username);
                    List<Menu> mes = menuDao.findByMenuId(map);
                    menu.setChildren(mes);
                }
            }

            //只是子菜单没有主菜单的情况
            List<Integer> submenuId = menuDao.submenu(username);
            if(submenuId != null && submenuId.size()>0){

                for (Integer integer : submenuId) {
                    Menu menu = menuDao.findByMenu(integer);
                    Map map = new HashMap();
                    map.put("id",integer);
                    map.put("username",username);
                    List<Menu> mes = menuDao.findByMenuId(map);
                    menu.setChildren(mes);
                    menus.add(menu);
                }

            }
            jedisPool.getResource().set("menu"+user.getId()+username,new ObjectMapper().writeValueAsString(menus));
            return menus;
        }else {
            return new ObjectMapper().readValue(menuRedis,List.class);
        }

    }

    @Override
    public List<Menu> getMenu(String username, Integer id) throws Exception {

        User user = userService.findByUserName(username);
        //加唯一标识
        String menuRedis = jedisPool.getResource().get("menuRoleId"+user.getId()+username+id);
        if(StringUtils.isEmpty(menuRedis)){
            Map map = new HashMap();
            map.put("username",username);
            map.put("roleId",id);
            List<Menu> menus = menuDao.findByUserNameByRoleId(map);
            for (Menu menu : menus) {
                //查找子菜单
                Integer parentMenuId = menu.getParentMenuId();
                if(parentMenuId == null){
                    Integer Roleid = menu.getId();
                    Map ma = new HashMap();
                    ma.put("id",Roleid);
                    ma.put("username",username);
                    ma.put("roleId",id);
                    List<Menu> mes = menuDao.findByMenuIdByRoleId(ma);
                    menu.setChildren(mes);
                }
            }

            //只是子菜单没有主菜单的情况
            List<Integer> submenuId = menuDao.submenudByRoleId(map);
            for (Integer integer : submenuId) {
                Menu menu = menuDao.findByMenu(integer);
                Map ma = new HashMap();
                ma.put("id",integer);
                ma.put("username",username);
                ma.put("roleId",id);
                List<Menu> mes = menuDao.findByMenuIdByRoleId(ma);
                menu.setChildren(mes);
                menus.add(menu);
            }
            jedisPool.getResource().set("menuRoleId"+user.getId()+username+id,new ObjectMapper().writeValueAsString(menus));
            return menus;
        }else {
            return new ObjectMapper().readValue(menuRedis,List.class);
        }
    }
    //分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);

        Page<Menu> menus = menuDao.findPage(queryString);
        long total = menus.getTotal();
        List<Menu> result = menus.getResult();
        return new PageResult(total,result);
    }

    @Override
    public List<Menu> findById(Integer id) {
        return menuDao.findById(id);
    }

    //回显数据
    @Override
    public String getRoleName(String username) {
        return menuDao.getRoleName(username);
    }


    @Override
    public Portrait getimg(String username) {
        return menuDao.getimg(username);
    }

    @Override
    public void edit(Map map) {
        //把照片存到缓存中
        String img = String.valueOf(map.get("img"));
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        menuDao.edit(map);
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuDao.findAllMenu();
    }

    @Override
    public List<Integer> findAllByMenu(Integer id) {
        return menuDao.findByRoleId(id);
    }

    //下拉列表角色
    @Override
    public List<Map> getrole(String username) {

        List<Role> roles = menuDao.getrole(username);
        List<Map> list = new ArrayList<>();
        for (Role role : roles) {
            Map map = new HashMap();
            map.put("value",role.getId());
            map.put("label",role.getName());
            list.add(map);
        }
        return list;
    }

}
